package com.ruoyi.develop.projectFeeInfo.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.IProjectBaseInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.projectFeeInfo.domain.ProjectFeeInfo;
import com.ruoyi.develop.projectFeeInfo.service.IProjectFeeInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目研发经费Controller
 * @author qh
 * @date 2023-01-10
 */
@RestController
@RequestMapping("/projectFeeInfo/projectFeeInfo")
public class ProjectFeeInfoController extends BaseController {
    @Autowired
    private IProjectFeeInfoService projectFeeInfoService;
    @Autowired
    private IProjectBaseInfoService projectBaseInfoService;

    /**
     * 查询项目研发经费列表
     */
    @PreAuthorize("@ss.hasPermi('develop:ProjectFeeInfo:query')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectFeeInfo entity) {
        startPage();
        List<ProjectFeeInfo> list = projectFeeInfoService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出项目研发经费列表
     */
    @PreAuthorize("@ss.hasPermi('develop:ProjectFeeInfo:export')")
    @Log(title = "项目研发经费", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectFeeInfo entity) {
        List<ProjectFeeInfo> list = projectFeeInfoService.selectList(entity);
        ExcelUtil<ProjectFeeInfo> util = new ExcelUtil<ProjectFeeInfo>(ProjectFeeInfo. class);
        util.exportExcel(response, list, "项目研发经费数据");
    }

    /**
     * 获取项目研发经费详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:ProjectFeeInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(projectFeeInfoService.selectById(id));
    }

    /**
     * 新增项目研发经费
     */
    @PreAuthorize("@ss.hasPermi('develop:ProjectFeeInfo:add')")
    @Log(title = "项目研发经费", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProjectFeeInfo entity) {
        if(entity.getFeeMoney().compareTo(BigDecimal.ZERO)!=0){
            projectFeeInfoService.insert(entity);
            ProjectFeeInfo projectFeeInfo2=new ProjectFeeInfo();
            projectFeeInfo2.setProjectId(entity.getProjectId());
            List<ProjectFeeInfo> projectFeeInfoList=projectFeeInfoService.selectList(projectFeeInfo2);
            BigDecimal allMoney = BigDecimal.ZERO;
            for(ProjectFeeInfo projectFeeInfo1:projectFeeInfoList){
                if (projectFeeInfo1.getFeeMoney() != null) {
                    allMoney = projectFeeInfo1.getFeeMoney().add(allMoney);
                }
            }
            ProjectBaseInfo projectBaseInfo=projectBaseInfoService.selectById(entity.getProjectId());
            projectBaseInfo.setFeeMoney(allMoney);
            projectBaseInfoService.update(projectBaseInfo);
            return null;
        }else {
            return null;
        }
    }

    /**
     * 修改项目研发经费
     */
    @PreAuthorize("@ss.hasPermi('develop:ProjectFeeInfo:edit')")
    @Log(title = "项目研发经费", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ProjectFeeInfo entity) {
        return toAjax(projectFeeInfoService.update(entity));
    }

    /**
     * 删除项目研发经费
     */
    @PreAuthorize("@ss.hasPermi('develop:ProjectFeeInfo:remove')")
    @Log(title = "项目研发经费", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        projectFeeInfoService.deleteByIds(ids);
        for(String id:ids){
            ProjectFeeInfo projectFeeInfo=projectFeeInfoService.selectById(id);
            if(projectFeeInfo!=null) {
                ProjectFeeInfo projectFeeInfo2=new ProjectFeeInfo();
                projectFeeInfo2.setProjectId(projectFeeInfo.getProjectId());
                List<ProjectFeeInfo> projectFeeInfoList=projectFeeInfoService.selectList(projectFeeInfo2);
                BigDecimal allMoney = BigDecimal.ZERO;
                for(ProjectFeeInfo projectFeeInfo1:projectFeeInfoList){
                    if (projectFeeInfo1.getFeeMoney() != null) {
                        allMoney = projectFeeInfo1.getFeeMoney().add(allMoney);
                    }
                }
                ProjectBaseInfo projectBaseInfo=projectBaseInfoService.selectById(projectFeeInfo.getProjectId());
                projectBaseInfo.setFeeMoney(allMoney);
                projectBaseInfoService.update(projectBaseInfo);
            }
        }
        return null;
    }
}
